package com.wbao.dianniu.tabstrip.fragments;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.lhh.apst.library.AdvancedPagerSlidingTabStrip;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.fragment.LazyLoadBaseFragment;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.ui.AmQuestionActivity;
import com.wbao.dianniu.ui.AmSearchActivity;
import com.wbao.dianniu.ui.AskActivity;
import com.wbao.dianniu.ui.MyKnowledgeActivity;
import com.wbao.dianniu.ui.QaSearchActivity;
import com.wbao.dianniu.R;
import com.wbao.dianniu.update.ErrorDialogHelper;

/**
 * 问答
 *
 * @author yejianjun
 */
public class FirstFragment extends LazyLoadBaseFragment implements OnPageChangeListener {
    public AdvancedPagerSlidingTabStrip mAPSTS;
    public APSTSViewPager mVP;

    private static final int VIEW_ANONY = 0;
    private static final int VIEW_QA = 1;
    private static final int VIEW_KNOW = 2;

    private static final int VIEW_SIZE = 3;

    private QAFragment qaFragment = null;
    private AmFragment amFragment = null;
    private KnowFragment knowFragment = null;

    private FragmentAdapter adapter;
    private ImageButton searchBtn;
    private ImageButton releaseBtn;
    private Dialog dialog;
    private int CurrentTab = 0;

    public static FirstFragment instance(Context context) {
        FirstFragment view = new FirstFragment();
        return view;

    }

    /*@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fist_pager, null);
        searchBtn = (ImageButton) view.findViewById(R.id.search_button);
        releaseBtn = (ImageButton) view.findViewById(R.id.release_button);
        mAPSTS = (AdvancedPagerSlidingTabStrip) view.findViewById(R.id.fistview_tabs);
        mVP = (APSTSViewPager) view.findViewById(R.id.fistview_apsts);
        mVP.setOffscreenPageLimit(VIEW_SIZE);

        mVP.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mAPSTS.setIndicatorHeight(4);
        mAPSTS.setViewPager(mVP);
        mAPSTS.setOnPageChangeListener(this);
        searchBtn.setOnClickListener(clickListener);
        releaseBtn.setOnClickListener(clickListener);
        return view;
    }*/

    OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.search_button:
                    Intent intent = new Intent();
                    if (VIEW_ANONY == CurrentTab) {
                        intent.setClass(getActivity(), AmSearchActivity.class);
                    } else if (VIEW_QA == CurrentTab) {
                        intent.setClass(getActivity(), QaSearchActivity.class);
                    } else if (VIEW_KNOW == CurrentTab) {
                        intent.setClass(getActivity(), QaSearchActivity.class);
                    }
                    startActivity(intent);
                    break;
                case R.id.release_button: // 提问按钮
//				showRealase();
                    if (VIEW_ANONY == CurrentTab) {// 当前处于匿名页 跳转到匿名提问
                        Intent anonymous = new Intent();
                        anonymous.setClass(getActivity(), AmQuestionActivity.class);
                        startActivity(anonymous);
                    } else if (VIEW_QA == CurrentTab) {
                        if (checkLevel()) {
                            Intent ask = new Intent();
                            ask.setClass(getActivity(), AskActivity.class);
                            startActivity(ask);
                        } else {
                            ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
                        }
                    } else if (VIEW_KNOW == CurrentTab) {
                        if (checkLevel()) {
                            Intent knowIntent = new Intent();
                            knowIntent.setClass(getActivity(), MyKnowledgeActivity.class);
                            startActivity(knowIntent);
                        } else {
                            ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public boolean checkLevel() {
        boolean ret = false;
        if (GlobalContext.getPerfectData()) {
            // 提示用户完成认证 必须达到一级认证才能提问
            ret = true;
        }
        return ret;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new FragmentAdapter(getChildFragmentManager());
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private class FragmentAdapter extends FragmentStatePagerAdapter {
        protected LayoutInflater mInflater;

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(getActivity());
        }


        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            if (position >= 0 && position < VIEW_SIZE) {
                switch (position) {
                    case VIEW_QA:
                        if (null == qaFragment)
                            qaFragment = QAFragment.instance();
                        return qaFragment;
                    case VIEW_ANONY:
                        if (null == amFragment)
                            amFragment = AmFragment.instance();
                        return amFragment;
                    case VIEW_KNOW:
                        if (null == knowFragment)
                            knowFragment = KnowFragment.instance();
                        return knowFragment;
                    default:
                        break;
                }
            }

            return null;
        }

        @Override
        public int getCount() {
            return VIEW_SIZE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case VIEW_QA:
                    return getResources().getString(R.string.dn_QandA);
                case VIEW_ANONY:
                    return getResources().getString(R.string.anonymous_title);
                case VIEW_KNOW:
                    return getResources().getString(R.string.knowledge_title);
                default:
                    break;
            }

            return super.getPageTitle(position);
        }

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int arg0) {
        CurrentTab = arg0;
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("FirstFragment"); //统计页面，"MainScreen"为页面名称，可自定义
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("FirstFragment");
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_fist_pager;
    }

    @Override
    protected void initView(View view) {
        searchBtn = (ImageButton) view.findViewById(R.id.search_button);
        releaseBtn = (ImageButton) view.findViewById(R.id.release_button);
        mAPSTS = (AdvancedPagerSlidingTabStrip) view.findViewById(R.id.fistview_tabs);
        mVP = (APSTSViewPager) view.findViewById(R.id.fistview_apsts);
        mVP.setOffscreenPageLimit(VIEW_SIZE);

        mVP.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mAPSTS.setIndicatorHeight(4);
        mAPSTS.setViewPager(mVP);
        mAPSTS.setOnPageChangeListener(this);
        searchBtn.setOnClickListener(clickListener);
        releaseBtn.setOnClickListener(clickListener);
    }
}
